package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.query.StrategyThemeCopyQueryObject;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IRegionService regionService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IStrategyThemeCopyService strategyThemeCopyService;

    @Reference
    private IRecommendService recommendService;

    @RequestMapping("")
    public String list(Model model) {
        model.addAttribute("hotRegions", regionService.queryHotRegions());
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public Object getHotDestByRegionId(Model model, Long regionId) {
        List<Destination> destinations = destinationService.queryDestByRegionId(regionId);
        if (destinations != null && destinations.size() > 0) {
            List<Destination> leftList = destinations.subList(0, destinations.size() / 2 + 1);
            List<Destination> rightList = destinations.subList(destinations.size() / 2 + 1, destinations.size());
            model.addAttribute("leftDests", leftList);
            model.addAttribute("rightDests", rightList);
        }
        model.addAttribute("regionId", regionId);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public Object guide(Model model, Long id) {
        setToastsToModel(model, id);
        // catalogs
        model.addAttribute("catalogs", strategyCatalogService.queryCatalogByDestId(id));
        //strategyDetails
        model.addAttribute("strategyDetails", strategyDetailService.queryDetailTop3(id));
        return "destination/guide";
    }

    /**
     * 设置参数
     *
     * @param model
     * @param id
     */
    private void setToastsToModel(Model model, Long id) {
        List<Destination> toasts = destinationService.getToasts(id);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
    }

    @RequestMapping("/surveyPage")
    public Object surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        setToastsToModel(model, qo.getDestId());
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public Object survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        for (StrategyCatalog strategyCatalog : strategyCatalogs) {
            if (strategyCatalog.getId().equals(qo.getCatalogId())) {
                model.addAttribute("catalog", strategyCatalog);
                StrategyDetail detail = strategyCatalog.getDetails().get(0);
                detail.setStrategyContent(strategyDetailService.getContentById(detail.getId()));
                model.addAttribute("detail", detail);
                break;
            }
        }
        model.addAttribute("catalogs", strategyCatalogs);
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model, TravelQuery qo) {
        PageInfo<Travel> travels = travelService.queryForList(qo);
        model.addAttribute("pageInfo", travels);
        return "destination/travelTpl";
    }

    /**
     * 按照主题过滤查询
     * @param
     */
    @RequestMapping("/filter")
    public String filter(Model model, @ModelAttribute("qo") StrategyThemeCopyQueryObject qo, Long themeId){
        //共享所有到页面:Themes
        qo.setType("全年适宜");
        List<StrategyThemeCopy> themesAll =  strategyThemeCopyService.selectAllThemesByType(qo);
        model.addAttribute("themesAll",themesAll);
        //themeSeason
        qo.setType("季节");
        List<StrategyThemeCopy> themeSeason =  strategyThemeCopyService.selectAllThemesByType(qo);
        model.addAttribute("themeSeason",themeSeason);
        //themeLeaveType
        qo.setType("出行方式");
        List<StrategyThemeCopy> themeLeaveType =  strategyThemeCopyService.selectAllThemesByType(qo);
        model.addAttribute("themeLeaveType",themeLeaveType);

        //根据所选的主题id查询出推荐的攻略信息
        qo.setThemeId(themeId);
        List<Recommend> destination =  recommendService.selectAllByThemeId(qo);
        model.addAttribute("destination",destination);

        return "destination/travelFilter";
    }


    //根据主题id去查询
    @RequestMapping("/themeAndDays")
    public String themeAndDays(Model model, @ModelAttribute("qo") StrategyThemeCopyQueryObject qo){

        //根据主题id查询出推荐信息
        List<Recommend> filter =  recommendService.selectAllByThemeId(qo);
        model.addAttribute("filter",filter);
        return "destination/travelFilterTpl";
    }
}
